package com.yyfax.ph.products;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.yyfax.common.adapter.StatesAdapter;
import com.yyfax.common.recyclerview.IRecyclerView;
import com.yyfax.common.recyclerview.OnItemClickListener;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.R;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.BaseFragment;
import com.yyfax.ph.base.YJPH;

import java.util.List;

/**
 * 产品页
 * Created by ying_he on 2017/10/27.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class ProductsFragment extends BaseFragment implements ProductsPresenter.View, RadioGroup.OnCheckedChangeListener
		, OnItemClickListener, StatesAdapter.RetryClickListener {

	private static final int LOADER_ID = 12211;
	@BindView(R.id.products_type_select_rg)
	RadioGroup mProductsTypeSelectRg;
	@BindView(R.id.products_lv)
	IRecyclerView mProductsLv;
	private Unbinder mUnbinder;
	private ProductsAdapter mAdapter;
	private ProductsPresenter mPresenter;
	private boolean mIsHotProducts = true;
	private ProductsModel.Products mProducts;
	private List<ProductsModel.Product> mProductData;
	private StatesAdapter mStatesAdapter;

	public static ProductsFragment newInstance() {
		return new ProductsFragment();
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_produces, null);
		mUnbinder = ButterKnife.bind(this, view);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mProductsTypeSelectRg.setOnCheckedChangeListener(this);
		mProductsLv.setLayoutManager(new GridLayoutManager(getContext(), 1));
//		mProductsLv.addItemDecoration(new Divider(DensityUtil.dip2px(getContext(), 5f), getResources().getColor(R.color.color_ededed), false, 0, 0, 0, 0, 0));
		mAdapter = new ProductsAdapter(this);
		mAdapter.setOnItemClickListener(this);
		View noNetworkLayout = getLayoutInflater().inflate(R.layout.state_view_no_network, mProductsLv, false);
		View emptyLayout = getLayoutInflater().inflate(R.layout.state_view_no_data, mProductsLv, false);
		mStatesAdapter = new StatesAdapter(mAdapter, null, emptyLayout, noNetworkLayout, mProductsLv, getContext());
		mStatesAdapter.setRetryClickListener(this);
		mProductsLv.setIAdapter(mStatesAdapter);
		getActivity().getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(getActivity(), new ProductsPresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		if (mPresenter == null) {
			mPresenter = (ProductsPresenter) data;
			mPresenter.attach(this);
			requestData();
		}
	}

	public void initData() {
		StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_C001);
		if (mAdapter != null) {
			List data = mAdapter.getDataSet();
			if (data == null || data.size() <= 0) {
				requestData();
			}
		}
	}

	public void requestData() {
		if (mPresenter != null) {
			mPresenter.getProducts();
		}
	}

	@Override
	public void onGetProductsSuccess(ProductsModel.Products products) {
		mProducts = products;
		notifyDataSetChanged();
	}

	@Override
	public void onGetProductsFailure(String tips) {
		showToast(tips);
		mStatesAdapter.setState(StatesAdapter.STATE_ERROR);//请求失败，显示网络异常点击重试页
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (mProducts == null) {
			requestData();
			return;
		}
		mIsHotProducts = checkedId == R.id.hot_products_btn;
		notifyDataSetChanged();
		String eventId = mIsHotProducts ? StatIDSet.EVENT_ID_C002 : StatIDSet.EVENT_ID_C003;
		StatService.onEvent(YJPH.getInstance(), eventId);

	}

	void notifyDataSetChanged() {
		mProductData = mIsHotProducts ? mProducts.hot : mProducts.more;
		mStatesAdapter.refreshData(mProductData);
	}

	@Override
	public void onItemClick(int position, Object o, View v) {
		ProductsModel.Product product = (ProductsModel.Product) o;
		StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_C004,product.name);
		ProductDetailActivity.start(getActivity(), product.productUrl, getString(R.string.product_detail_introduce, product.name));
	}

	@Override
	public void onRetry() {
		requestData();
	}

	@Override
	public void onFunctionalError(Throwable e, boolean autoTips) {
		super.onFunctionalError(e, autoTips);
		getActivity().runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mStatesAdapter.setState(StatesAdapter.STATE_ERROR);
			}
		});
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		mUnbinder.unbind();
		if (mStatesAdapter != null) {
			mStatesAdapter.dettach();
		}
		getActivity().getSupportLoaderManager().destroyLoader(LOADER_ID);
	}

}